package view;

import inventory.InventoryItem;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Model;
import model.UserPage;

/**
 * UserPageFriendsXML implements the Friends XML display of a user's signed-in
 * dashboard, populating the display the listings and information of a user's
 * fiends list.
 *
 * Primarily used for the Android side of the Lithub project.
 * 
 * @author Reynaldo Gonzales.
 * @version 1.0
 *
 */
class UserPageFriendsXML extends View {

    private Model model;
    private HttpServletRequest request;
    private HttpServletResponse response;

    /**
     * UserPageFriendsXML(...) is a reciever of user dashboard page view
     * requests for the web based Friends Div in XML and also the responder to
     * such as well.
     *
     * @param a HttpServletRequest request which holds the HTTP request
     * information of the requesting user.
     *
     * @param b HttpServletResponse response to the requesting user.
     *
     */
    UserPageFriendsXML(HttpServletRequest req, HttpServletResponse res) {
        request = req;
        response = res;
    }

    /**
     * invoke(modem m) is a model invoker which casts the invokation of the
     * UserPage/Friends page model implementation in XML to try to provide a user
     * with a populated and informatinal friends list corresponding to the
     * friends list they have associated with their account.
     *
     * @param a model which refers to the model that implements the
     * UserPageDiscoverDiv.
     *
     * @catch generic exception.
     *
     */
    @Override
    public void invoke(Model m) {
        PrintWriter out = null;
        try {
            UserPage u = (UserPage) m;

            out = response.getWriter();
            response.setContentType("text/xml");
            out.print("<body copyright='All data copyright LitHub 2012'>");

            for (int i = 0; i < u.user.getFriendListSize(); ++i) {
                out.print("<friend>");
                out.print("<username>" + u.user.getFriend(i).username + "</username>");
                out.print("<userID>" + u.user.getFriend(i).id + "</userID>");
                out.print("</friend>");
            }
            out.print("</body>");
        } catch (Exception ex) {
            Logger.getLogger(FrontPageHTML.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
